const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo:{},
    inputShow:false,
    userName:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo:wx.getStorageSync('storeInfo')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  gotAddress(){
    wx.navigateTo({
      url: '/pages/address/index',
    })
  },
  nameTap(){
    this.setData({
      inputShow:true
    })
  },
  cancelTap(){
    this.setData({
      inputShow:false
    })
  },
  completeTap(){
    let data = {
      image:wx.getStorageSync('storeInfo').image,
      name:this.data.userName,
      storeId:wx.getStorageSync('userInfo').storeId
    }
    this.storeUpdName(data)
  },
  // 点击换头像
  onChooseAvatar(e) {
    let that = this
    wx.showLoading({
      title: '加载中',
      mask: true,
    })
    console.log(e)
    wx.uploadFile({
      url: app.agriknow._baseUrl + 'ht/user/uploadImage', 
      filePath: e.detail.avatarUrl,
      name: 'file',
      header: {
        'token': wx.getStorageSync('token')
      },
      formData: {
        'file': e.detail.avatarUrl
      },
      success(res) {
        let data = {
          image:JSON.parse(res.data).data,
          name:wx.getStorageSync('storeInfo').name,
          storeId:wx.getStorageSync('userInfo').storeId
        }
        that.storeUpdName(data)
        wx.hideLoading()
      },
      fail(res){
        wx.hideLoading()
        let resData = JSON.parse(res.data)
        that.setShowModal(resData.msg)
      }
    })
  },
  storeUpdName(data){
    app.agriknow.storeUpdName(data).then(res=>{
      if(res.code=='000000'){
        this.storeUserHome()
      }else{
        wx.showToast({
          title: res.message,
          icon:'none'
        })
      }
    })
  },
  storeUserHome(){
    let data = {
      id:wx.getStorageSync('userInfo').storeId
    }
    app.agriknow.storeUserHome(data).then(res=>{
      wx.setStorageSync('storeInfo',res.data)
      this.setData({
        userInfo:wx.getStorageSync('storeInfo'),
        inputShow:false,
        userName:''
      })
      wx.showToast({
        title: '修改成功',
        icon:'none'
      })
    })
  },
  setShowModal(text){
    wx.showModal({
      content: text,
      showCancel:false,
      success (res) {
        if (res.confirm) {
        } else if (res.cancel) {
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})